package com.af.data.jpa.util;

import com.af.system.api.page.AfResponsePage;
import com.af.system.api.page.AfResponsePageEx;
import org.springframework.data.domain.Page;

/**
 * @author : zhenyun.su
 * @since : 2019/1/8
 * @commit: return AfResponsePage or AfResponsePageEx; converter Spring Page
 */

public class AfResponsePageUtils {

    public static AfResponsePage of() { return new AfResponsePage();
    }

    public static AfResponsePage of(Page page) {
        AfResponsePage AfResponsePage = new AfResponsePage();
        AfResponsePage.setContent(page.getContent());
        AfResponsePage.setRealSize(page.getNumberOfElements());
        AfResponsePage.setTotalSize(page.getTotalElements());
        return AfResponsePage;
    }

    public static AfResponsePageEx ofEx() { return new AfResponsePageEx();
    }


    public static AfResponsePageEx ofEx(Page page) {
        AfResponsePageEx AfResponsePage = new AfResponsePageEx();
        AfResponsePage.setContent(page.getContent());
        AfResponsePage.setFirst(page.isFirst());
        AfResponsePage.setLast(page.isLast());
        AfResponsePage.setPaged(page.getPageable().isPaged());
        AfResponsePage.setSorted(page.getPageable().getSort().isSorted());
        AfResponsePage.setRealPage(page.getNumber());
        AfResponsePage.setRealSize(page.getNumberOfElements());
        AfResponsePage.setTotalPage(page.getTotalPages());
        AfResponsePage.setTotalSize(page.getTotalElements());
        return AfResponsePage;
    }
}
